<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        window.localStorage.setItem('a','abc')
        window.localStorage.setItem('b',JSON.stringify({a:1}))
        function setItem(key,value){
            if(typeof value === 'object'){
                value = JSON.stringify(value)
            }
            window.localStorage.setItem(key,value)
        }

        // window.localStorage.getItem('a')
        // console.log(JSON.parse(window.localStorage.getItem('b')))
        // try{
        //     JSON.parse(window.localStorage.getItem('b'))
        // }catch(err){
        //     window.localStorage.getItem('b')
        // }
        
        //把可能会出错的放在try里面
        try{
            console.log(JSON.parse(window.localStorage.getItem('a')))
        }catch(err){
            console.log(window.localStorage.getItem('a'))
        }
    </script>
</body>
</html>